home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 1995 July & August / cd No4 joystick No62.iso / mac / pc / SHARE / LIGHTLIB / LANGUAGE.Z / LLIDDOC.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  14KB  |  392 lines

  1. // LLIDdoc.cpp : implementation of the CLLIDDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. extern "C"        // include your LightLibImage - header file here
  7. {
  8. #include "LLI.H"
  9. }
  10.  
  11.  
  12. #include "LLID.h"
  13.  
  14. #include "LLIDdoc.h"
  15. #include "LLIDview.h" // added to access to the view
  16.  
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CLLIDDoc
  25.  
  26. IMPLEMENT_DYNCREATE(CLLIDDoc, CDocument)
  27.  
  28. BEGIN_MESSAGE_MAP(CLLIDDoc, CDocument)
  29.  //{{AFX_MSG_MAP(CLLIDDoc)
  30.     ON_COMMAND(ID_GEN_MOSAIC, OnGenMosaic)
  31.     //}}AFX_MSG_MAP
  32. END_MESSAGE_MAP()
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // CLLIDDoc construction/destruction
  36.  
  37. CLLIDDoc::CLLIDDoc()
  38. {
  39.     // TODO: add one-time construction code here
  40.     
  41.     lloImage = 0;
  42.     lloOrigImage = 0;
  43. }
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46.  
  47. CLLIDDoc::~CLLIDDoc()
  48. {                                              
  49.     // do clean up
  50.     if( lloImage )
  51.         lloImage = oDel( lloImage );
  52.     if( lloOrigImage )
  53.         lloOrigImage = oDel( lloOrigImage );
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57.  
  58. BOOL CLLIDDoc::OnNewDocument()
  59. {
  60.     // we will create a new image by reading it from scanner
  61.     
  62.     // Load new cursor and sve the old one                  
  63.     HCURSOR oldCursor;
  64.     oldCursor = SetCursor( LoadCursor( 0, IDC_WAIT ) );
  65.     
  66.  
  67.     POSITION pos = GetFirstViewPosition();
  68.     CLLIDView* pView = (CLLIDView *)GetNextView( pos );
  69.     
  70.     pView->IdleStart(20);
  71.     lloImage = iGet( lloApp,            // Applic.handle created with oNew
  72.                      LLI_SCANNER,       // source : read from scanner
  73.                      LLI_SCANNER_TWAIN, // scanner type 
  74.                      0,                 // upper left corner X
  75.                      0,                 // upper left corner Y
  76.                      -1,                // lower right corner X
  77.                      -1,                // lower right corner Y
  78.                      0,                 // density
  79.                      0,                 // luminosity
  80.                      0,                 // contrast
  81.                      0,                 // - not used -
  82.                      0,                 // - not used -
  83.                      (DWORD)pView );    // userparameter
  84.     pView->IdleStop();                 
  85.     
  86.     
  87.                      
  88.     // retreive old cursor
  89.     SetCursor ( oldCursor );
  90.     
  91.     if(! lloImage )
  92.     {
  93.         lloImage = iGet( lloApp,      // LightLibObject application handle
  94.             LLI_MEMORY,    // source
  95.             LLI_MEMORY_16, // 16 color mode
  96.             0,             // upper left corner X
  97.             0,             // upper left corner Y
  98.             100,           // lower right corner X
  99.             100,           // lower right corner Y
  100.             0,             //  - not used -
  101.             0,             //  - not used -
  102.             0,             //  - not used -
  103.             0,             //  - not used -
  104.             0,             //  - not used -
  105.             0 );           //  - not used -
  106.     }                                              
  107.     else
  108.     {
  109.         // don't allow images with more colors than display can handle
  110.         
  111.         short val = (short)oAccess( lloImage, LLI_IMAGE_BITS, 0 );
  112.         CDC dc;
  113.         int ScreenCap = dc.GetDeviceCaps( DT_RASDISPLAY );
  114.         
  115.         if ( (val == 24) && ( ScreenCap < 16 ) )
  116.         {
  117.             AfxMessageBox( (LPCSTR)"Video settings too low"
  118.                            "Too many colors for this image to be displayed" );
  119.                            
  120.             CDocument::OnNewDocument();
  121.             return FALSE;
  122.         }
  123.         else // if videomode is <= 256 colors then use shared palette, else use exclusive one
  124.         {       
  125.             CLLIDView* pView = (CLLIDView *)GetNextView( pos );
  126.  
  127.             if ( ScreenCap <= 256 )
  128.             {
  129.                 pView->ExclusivePaletteIsActive = LLI_PALETTE_EXCLUSIVE;
  130.             }
  131.             else
  132.             {
  133.                 pView->ExclusivePaletteIsActive = LLI_PALETTE_SHARED;
  134.             }
  135.         }
  136.         
  137.     }                          
  138.     
  139.     // keep a copy of original            
  140.     if( lloImage )
  141.     {
  142.         lloOrigImage = iCopy( lloImage,      // pointer to image
  143.                               0,             // upper left corner X
  144.                               0,             // upper left corner Y
  145.                               -1,            // lower right corner X
  146.                               -1,            // lower right corner Y
  147.                               LLI_COPY_CLONE,// just make a copy
  148.                               0,             // - not used in this case -
  149.                               0,             // - not used in this case -
  150.                               0,             // - not used in this case -
  151.                               0,             // - not used in this case -
  152.                               0,             // - not used in this case -
  153.                               0 );           // userparameter
  154.     }                              
  155.     CDocument::OnNewDocument();
  156.     return TRUE;
  157. }
  158.  
  159. /////////////////////////////////////////////////////////////////////////////
  160. // CLLIDDoc serialization
  161.  
  162. void CLLIDDoc::Serialize(CArchive& ar)
  163. {
  164.     if (ar.IsStoring())
  165.     {
  166.         // TODO: add storing code here
  167.     }
  168.     else
  169.     {
  170.         CDocTemplate * cTmp = GetDocTemplate();
  171.         // TODO: add loading code here
  172.     }
  173. }
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176. // CLLIDDoc diagnostics
  177.  
  178. #ifdef _DEBUG
  179.  
  180. void CLLIDDoc::AssertValid() const
  181. {
  182.     CDocument::AssertValid();
  183. }
  184.  
  185. /////////////////////////////////////////////////////////////////////////////
  186.  
  187. void CLLIDDoc::Dump(CDumpContext& dc) const
  188. {
  189.     CDocument::Dump(dc);
  190. }
  191.                            
  192. #endif //_DEBUG
  193.  
  194. /////////////////////////////////////////////////////////////////////////////
  195. // CLLIDDoc commands
  196.  
  197. void CLLIDDoc::DeleteContents() // clean up when closing or reopening a document
  198. {
  199.     if ( lloImage )
  200.     {
  201.         lloImage = oDel( lloImage );
  202.     }
  203.     if ( lloOrigImage ) 
  204.     {
  205.         lloOrigImage = oDel( lloOrigImage );
  206.     }
  207. }
  208.  
  209. /////////////////////////////////////////////////////////////////////////////
  210.  
  211. BOOL CLLIDDoc::OnOpenDocument( const char *pszPathName )
  212. {
  213.     // TODO: Add your command handler code here          
  214.     
  215.     // read an image from disk
  216.     
  217.     
  218.     POSITION pos = GetFirstViewPosition();
  219.     CLLIDView* pView = (CLLIDView *)GetNextView( pos );
  220.     
  221.     pView->IdleStart(20);                  
  222.     lloImage = iGet( lloApp,             // Applic.handle created with oNew
  223.                      LLI_DISK,           // source : read from disk
  224.                      0,                  // format : autodetect
  225.                      0,                  // upper left corner X
  226.                      0,                  // upper left corner Y
  227.                      -1,                 // lower right corner X
  228.                      -1,                 // lower right corner Y
  229.                      (DWORD)pszPathName, // path and filename
  230.                      0,                  // - not used in this case -
  231.                      0,                  // - not used in this case -
  232.                      0,                  // - not used in this case -
  233.                      0,                  // - not used in this case -
  234.                      (DWORD)pView );     // userparameter
  235.     pView->IdleStop();
  236.                      
  237.     if(! lloImage )
  238.     {
  239.         lloImage = iGet( lloApp,      // LightLibObject application handle
  240.             LLI_MEMORY,    // source
  241.             LLI_MEMORY_16, // 16 color mode
  242.             0,             // upper left corner X
  243.             0,             // upper left corner Y
  244.             100,           // lower right corner X
  245.             100,           // lower right corner Y
  246.             0,             //  - not used -
  247.             0,             //  - not used -
  248.             0,             //  - not used -
  249.             0,             //  - not used -
  250.             0,             //  - not used -
  251.             0 );           //  - not used -
  252.     }
  253.  
  254.     // if load was successful then keep a copy of original            
  255.     lloOrigImage = iCopy( lloImage,      // pointer to image
  256.                           0,             // upper left corner X
  257.                           0,             // upper left corner Y
  258.                           -1,            // lower right corner X
  259.                           -1,            // lower right corner Y
  260.                           LLI_COPY_CLONE,// just make a copy
  261.                           0,             // - not used in this case -
  262.                           0,             // - not used in this case -
  263.                           0,             // - not used in this case -
  264.                           0,             // - not used in this case -
  265.                           0,             // - not used in this case -
  266.                           0 );           // userparameter
  267.     return TRUE;
  268. }
  269.  
  270. ///////////////////////////////////////////////////////////////////////////////////////////
  271.  
  272. BOOL CLLIDDoc::OnSaveDocument( const char *pszPathName )
  273. {
  274.     // TODO: Add your command handler code here
  275.  
  276.     // We need a pointer to the corresponding view in order to get
  277.     // information about the selected capture region
  278.     
  279.     // To get the first view in the list of views assigned to this document:
  280.     POSITION pos = GetFirstViewPosition();
  281.     CLLIDView* pView = (CLLIDView *)GetNextView( pos );
  282.  
  283.     
  284.     if( lloImage )
  285.     {          
  286.         CRect rTmp;           
  287.         
  288.         rTmp = pView->GetSelection(); // get the selected capture rectangle
  289.  
  290.  
  291.         // To avoid the implementation of a class for each image type,
  292.         // we simply read the extension of the filename, the user typed
  293.         // in the SaveAs dialogbox and then make up a decision about the
  294.         // format the image will be saved in.
  295.         
  296.         DWORD iType;
  297.         char *ExtStr;
  298.         
  299.         // point to extension
  300.         ExtStr = (char*)pszPathName + strlen( pszPathName ) -4; 
  301.         
  302.         // compare extension
  303.         if ( strcmp( ExtStr,".GIF") == 0 ) iType = LLI_DISK_GIF;
  304.          else if ( strcmp( ExtStr, ".JPG") == 0 ) iType = LLI_DISK_JPG;
  305.           else if ( strcmp( ExtStr, ".PCX") == 0 ) iType = LLI_DISK_PCX;
  306.            else if ( strcmp( ExtStr, ".TIF") == 0 ) iType = LLI_DISK_TIF;
  307.             else iType = LLI_DISK_BMP;
  308.         // if we did not find a match, we simply save as a Bitmap (*.BMP)
  309.  
  310.         
  311.         pView->IdleStart(20);
  312.         
  313.         iPut( lloImage,           // pointer to image
  314.               rTmp.left,          // upper left corner X
  315.               rTmp.top,           // upper left corner Y
  316.               rTmp.right,         // lower right corner X
  317.               rTmp.bottom,        // lower right corner Y
  318.               LLI_DISK,           // destination disk
  319.               iType,              // image type (format)
  320.               0,                  // horizontal offset in the file
  321.               0,                  // vertical offset in the file 
  322.               (DWORD)pszPathName, // path and filename
  323.               0,                  // compression type
  324.               0,                  // - not used in this case -
  325.               0,                  // - not used in this case -
  326.               0,                  // - not used in this case -
  327.               (DWORD)pView );     // userparameter
  328.               
  329.         pView->IdleStop();
  330.               
  331.         return TRUE;
  332.     }
  333.     return FALSE;
  334. }
  335.  
  336. ///////////////////////////////////////////////////////////////////////////////////////////
  337.  
  338. CSize CLLIDDoc::GetImageSize( BOOL OrigImage )
  339. {                                                     
  340.     //  Get the horizontal and vertical size of the image.
  341.     //  This method is used by the LLIDView class to obtain
  342.     //  either the size of the original image or the current
  343.     //  image (propably different)
  344.     CSize xy;
  345.                  
  346.     int xx=0, yy=0;
  347.     
  348.     if ( (!lloOrigImage) || (!lloImage) ) 
  349.     {
  350.         xy.cx = 0;
  351.         xy.cy = 0;
  352.         return xy;
  353.     }
  354.     
  355.     switch ( OrigImage )
  356.     {
  357.         case TRUE :
  358.             // the caller desires the original size
  359.             xx = (int)oAccess( lloOrigImage, LLI_IMAGE_WIDTH, 0 );         
  360.             yy = (int)oAccess( lloOrigImage, LLI_IMAGE_HEIGHT, 0 );
  361.             break;        
  362.             
  363.         case FALSE :
  364.         default:                                   
  365.             // the caller desires the current size
  366.             xx = (int)oAccess( lloImage, LLI_IMAGE_WIDTH, 0 );         
  367.             yy = (int)oAccess( lloImage, LLI_IMAGE_HEIGHT, 0 );
  368.     }                                                              
  369.     
  370.     xy.cx = xx;
  371.     xy.cy = yy;
  372.     return xy;
  373.  
  374. ///////////////////////////////////////////////////////////////////////////////////////////
  375.  
  376. void CLLIDDoc::OnGenMosaic()
  377. {
  378.     // we first create an empty image of a certain size and then copy
  379.     // all images currently loaded into it. ( tile )
  380.     
  381.     CSize mSize;
  382.     mSize.cx = 500;  //  the mosaic's width
  383.     mSize.cy = 300;  //  the mosaic's height
  384.     
  385.     // now get all the images currently open
  386.  
  387.     AfxMessageBox( (LPCSTR)"Option not yet available");
  388.     
  389. }
  390.  
  391.